<?php

require_once '../include/common.inc.php';
require_once '../include/upfile.class.php';
if (!checkAdmin()) {
    header("LOCATION:index.php");
    exit;
}
$menu_index = 3;
$page_title = 'TABLA';
$guide_message = 'Media | Promoter banner';

$sql = "SELECT id FROM `med_banner` order by order_index";
$query = $db->query($sql);
$sql_all_num = $db->num_rows($query);

getMyParam("action");
if($action !="" && $action !=null)
{
	for($i=1; $i <= $sql_all_num; $i++)
	{
		$order_index="orderIndex".$i;
		$old_pic= "old_pic" . $i;
		$old_pic_d= "old_pic" . $i ."_d";
		$remove_pic = "remove_pic" . $i;
		$remove_pic_d = "remove_pic" . $i ."_d";
		$curId="id".$i;		
		getMyParam($order_index);
		getMyParam($old_pic);
		getMyParam($old_pic_d);
		getMyParam($remove_pic);
		getMyParam($remove_pic_d);
		getMyParam($curId);
		
		if($$remove_pic=="del")
		{
			@unlink('../pictures/promotion/'.$$old_pic);
			$pic_up_name="`picurl` = '',";
		}
		else
			$pic_up_name="`picurl` = '".$$old_pic."',";
			
		if($$remove_pic_d=="del")
		{
			@unlink('../pictures/promotion/'.$$old_pic_d);
			$pic_up_name_d="`picurl_danish` = '',";
		}
		else
			$pic_up_name_d="`picurl_danish` = '".$$old_pic_d."',";	

		$curPic='PromotionBanner_'.$i;
		$curPic_d='PromotionBanner_'.$i."_d";
		if(isset($_FILES[$curPic]) && $_FILES[$curPic]['size'] > 0 && trim($_FILES[$curPic]['name']) != "")
		{
			$upfile = new uploadFile($_FILES[$curPic]);
			$upfile->setFileName($curPic);
			$pic_up_name="`picurl` = '$upfile->fileName',";
			$upfile->setFileType(array('image/pjpeg', 'image/gif', 'image/swf', 'image/fla'));
			$upfile->setAcceptOverwrite(0);   
			$upfile->setStoreDir('../pictures/promotion/');
			$upfile->setMaxSize(1024*1024*8);
			
			if(file_exists('../pictures/promotion/'.$upfile->fileName))
				@unlink('../pictures/promotion/'.$upfile->fileName);
			if (!$upfile->save()) {
			echo "<script>alert('".$curPic." ". $upfile->error_msg . "');location.href='m_banner.php';</script>";
			exit;
			}
			@unlink('../pictures/promotion/'.$$old_pic);
			unset($upfile);
		}
		if(isset($_FILES[$curPic_d]) && $_FILES[$curPic_d]['size'] > 0 && trim($_FILES[$curPic_d]['name']) != "")
		{
			$upfile = new uploadFile($_FILES[$curPic_d]);
			$upfile->setFileName($curPic_d);
			$pic_up_name_d="`picurl_danish` = '$upfile->fileName',";
			$upfile->setFileType(array('image/pjpeg', 'image/gif', 'image/swf', 'image/fla'));
			$upfile->setAcceptOverwrite(0);   
			$upfile->setStoreDir('../pictures/promotion/');
			$upfile->setMaxSize(1024*1024*8);
		
			if(file_exists('../pictures/promotion/'.$upfile->fileName))
				@unlink('../pictures/promotion/'.$upfile->fileName);
			if (!$upfile->save()) {
			echo "<script>alert('".$curPic_d." ". $upfile->error_msg . "');location.href='m_banner.php';</script>";
			exit;
			}
			@unlink('../pictures/promotion/'.$$old_pic_d);
			unset($upfile);
		}
		
		$sql="update `med_banner` set
			 $pic_up_name
			 $pic_up_name_d
			 `order_index`=".$$order_index." where id=".$$curId;
		$db->query($sql);
		
	}
}
$sql = "SELECT * FROM `med_banner` order by order_index";
$query = $db->query($sql);
while($rs=$db->fetch_array($query))
{
	if(trim($rs["picurl"])!="")
    	$rs["pic"]=getOutPut($rs["picurl"],$rs["width"],$rs["height"]);
	else
		$rs["pic"]="";
		
	if(trim($rs["picurl_danish"])!="")
    	$rs["pic_d"]=getOutPut($rs["picurl_danish"],$rs["width"],$rs["height"]);
	else
		$rs["pic_d"]="";	
	$ary[]=$rs;
}
include_once('menu_list.php');
require_once PrintEot('a_header');
require_once PrintEot('a_m_banner');
require_once PrintEot('a_footer');

?>
